package com.eastfair.matching.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.eastfair.auth.api.AuthServiceFeign;
import com.eastfair.auth.api.GlobalBusinessServiceFeign;
import com.eastfair.auth.api.RoleServiceFeign;
import com.eastfair.auth.api.UserAccountServiceFeign;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.jwt.service.JwtService;
import com.eastfair.matching.service.ComplementServiceProviderService;
import com.eastfair.venuebooking.api.ServiceProviderFeign;
import com.eastfair.venueservice.dto.ServiceProviderDTO;
import com.eastfair.venueservice.enumeration.ServiceProviderTypeEnum;
import com.eastfair.venueservice.vo.ServiceProviderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 业务实现类
 * 配套服务商信息
 * </p>
 *
 * @author lyw
 * @date 2022-07-15
 */
@Slf4j
@Service
public class ComplementServiceProviderServiceImpl implements ComplementServiceProviderService {

    @Autowired
    private ServiceProviderFeign serviceProviderFeign;

    @Autowired
    private UserAccountServiceFeign userAccountServiceFeign;
    @Autowired
    private AuthServiceFeign authServiceFeign;
    @Autowired
    private RoleServiceFeign roleServiceFeign;
    @Autowired
    private GlobalBusinessServiceFeign globalBusinessServiceFeign;
    @Autowired
    JwtService jwtService;

    /**
     * 查询配套服务商信息
     * @param userId
     * @return
     */
    @Override
    public ServiceProviderVO getComplementServiceProviderByUserId(Long userId) {
        log.info("ComplementServiceProviderServiceImpl +++++ getComplementServiceProviderByUserId - 查询配套服务商信息 params={}", userId);
        R<ServiceProviderVO> r = serviceProviderFeign.getByUserId(userId, ServiceProviderTypeEnum.MATCHING_SERVICE_PROVIDER.getCode());
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        return r.getData();
    }


    /**
     * 保存配套服务商信息
     * @param serviceProviderDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveComplementServiceProvider(ServiceProviderDTO serviceProviderDTO) {
        log.info("ComplementServiceProviderServiceImpl +++++ saveComplementServiceProvider - 保存配套服务商信息 params={}", JSONObject.toJSON(serviceProviderDTO));
        serviceProviderDTO.setServiceProviderType(ServiceProviderTypeEnum.MATCHING_SERVICE_PROVIDER.getCode());
        R<Boolean> r = serviceProviderFeign.submitServiceProvider(serviceProviderDTO);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }

        return r.getData();
    }

    /**
     * 修改配套服务商信息
     * @param serviceProviderDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateComplementServiceProvider(ServiceProviderDTO serviceProviderDTO) {
        log.info("ComplementServiceProviderServiceImpl +++++ updateComplementServiceProvider - 修改配套服务商信息 params={}", JSONObject.toJSON(serviceProviderDTO));
        serviceProviderDTO.setServiceProviderType(ServiceProviderTypeEnum.MATCHING_SERVICE_PROVIDER.getCode());
        R<Boolean> r = serviceProviderFeign.submitServiceProvider(serviceProviderDTO);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        return r.getData();
    }

}
